package com.netease.yanxuan.qa.yx.yuheng.controller;

import com.alibaba.fastjson.JSONObject;
import com.netease.yanxuan.qa.yx.yuheng.config.YxYuhengConstants;
import com.netease.yanxuan.qa.yx.yuheng.dto.TestCaseInfoDto;
import com.netease.yanxuan.qa.yx.yuheng.entity.TestCaseInfo;
import com.netease.yanxuan.qa.yx.yuheng.service.impl.TestCaseInfoServiceImpl;
import com.netease.yanxuan.qa.yx.yuheng.vo.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.netease.yanxuan.qa.yx.yuheng.config.YxYuhengConstants.SUCCESS_CODE;

/**
 * <Description>
 *
 * @author yanlu02
 * @date 2021/1/13 16:53
 */
@RestController
@RequestMapping(value = YxYuhengConstants.BASE_PATH + "/v1" + "/case")
@Api(value = "case-controller-v1", description = "用例信息")
@Slf4j
public class TestCaseInfoController {

    @Autowired
    private TestCaseInfoServiceImpl testCaseInfoService;

    @ApiOperation(value = "查询用例详情")
    @RequestMapping(value = "queryTestCases", method = RequestMethod.GET)
    public ApiResult<List<TestCaseInfo>> queryTestCases(
            @ApiParam(name = "casePath", value = "用例路径", defaultValue = "") @RequestParam String casePath,
            @ApiParam(name = "priority", value = "用例级别", defaultValue = "-1") @RequestParam byte priority,
            @ApiParam(name = "status", value = "用例状态", defaultValue = "-1") @RequestParam byte status) {
        log.info("queryTestCases request，casePath={}, priority={}, status={}", casePath, priority, status);
        List<TestCaseInfo> list = testCaseInfoService.queryTestCases(casePath, priority, status);
        return new ApiResult<List<TestCaseInfo>>().setCode(SUCCESS_CODE).setData(list);
    }

    @ApiOperation(value = "更新用例")
    @RequestMapping(value = "updateTestCase", method = RequestMethod.POST)
    public ApiResult<Boolean> updateTestCase(
            @ApiParam(name = "testCaseInfoDto", value = "case信息", required = true) @RequestBody TestCaseInfoDto testCaseInfoDto) {

        log.info("updateTestCase request，testCaseInfoDto={}", JSONObject.toJSON(testCaseInfoDto));
        Boolean result = testCaseInfoService.updateTestCase(testCaseInfoDto);
        return new ApiResult<Boolean>().setCode(SUCCESS_CODE).setData(result);
    }
}
